<?php
/**
*
* 功能描述（代金数据）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class ModelVoucher extends ModelBase
{
	function GetInfo($no)
	{
		//{#$v_sta=[1=>'新生成', 2=>'已打印', 3=>'已发放', 4=>'已使用', 5=>'已过期']#}
		$where = "`no` = '$no' and status >= 3";
		$sql = "select id, status, TIMESTAMPDIFF(MINUTE, CURRENT_TIMESTAMP, outtime) lefttime, amount from voucher where $where";
		$data = $this->db->getRowBySql($sql, "Row");
		if($data && $data['lefttime'] < 0 && $data['status'] == 3)	//过期
			$data['status'] = 5;
		return $data;
	}

	function UseById($id, $use_amount, $order_id, $user_id)
	{
		$data  = array(
			'status' => 4,
			'usetime' => date("Y-m-d H:i:s"),
			'amount_use' => $use_amount,
			'user_id' => $user_id,
			'order_id' => $order_id,
		);
		return $data = $this->db->Update('voucher', $data, "id = $id");
	}
}

